using System;
using System.Runtime.Serialization;



namespace Huazhu.PMS.Framework.Organization
{
    [DataContract]
    [Serializable]
    public sealed class OrganizationScope : ScopeBase
    {
        private OrganizationScope()
        {            
        }

        public OrganizationScope(string scopePrefix)
        {
            this._scopePrefix = scopePrefix;
            Organization.OrgTreeNode node = Organization.OrgTreeProvider.GetOrganizationTree().SelectNode(scopePrefix);
            if (node != null)
            {
                this._orgId = node.Id;
            }
        }

        public OrganizationScope(OrgTreeNode node)
        {
            if (node != null)
            {
                _orgId = node.Id;
                _scopePrefix = OrgTree.FindNodeById(_orgId).FullName;
            }
        }

        private string _orgId;

        [DataMember]
        public string OrgId
        {
            get { return _orgId; }
            set 
            { 
                _orgId = value;

                OrgTreeNode node = OrgTree.FindNodeById(_orgId);

                if(node != null)
                {
                    _scopePrefix = node.FullName;
                }
            }
        }

        public override bool Equals(object scope)
        {
            if (!(scope is OrganizationScope))
                return false;

            return _scopePrefix.Equals(((OrganizationScope)scope)._scopePrefix);
        }

        public override int GetHashCode()
        {
            return _scopePrefix == null ? 0 : _scopePrefix.GetHashCode();
        }
    }//end OrganizationScope
}